﻿using Petroleum.UI.Tool.Office;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// 注意：需要本地安装了Office

// dsoframer的原理是：利用ActiveX插件调用本地的Office
// 第1步：注册“dsoframer.ocx”
// 第2步：项目里添加Com组件：工具箱=》右键=》选择项=》Com组件=》DSO Framer Control Object=》确定
//          注意：一定要注册dsoframer.ocx后，才能在添加com组件时，找到"DSO Framer Control Object"
//          注意：如果没有注册dsoframer.ocx，无法正常将其添加到工具箱
// 第3步：编写代码，调用相关接口

namespace Petroleum
{
    public partial class Form1 : Form
    {
        private AxFramerControlSetter axFramerCS_One = null;

        public Form1()
        {
            InitializeComponent();
            this.button3.Enabled = false;
            this.button4.Enabled = false;
            this.button5.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (OcxRegister.IsRegisted())
            {
                this.button1.Enabled = false; //注册成功时，注册按钮不可点
                axFramerCS_One = new AxFramerControlSetter(new AxDSOFramer.AxFramerControl());
                this.button3.Enabled = true;
                this.button4.Enabled = true;
                this.button5.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //注册dsoframer.ocx
            int result = OcxRegister.RegisterMain();
            switch (result)
            {
                case -1:
                    MessageBox.Show("注册失败！");
                    break;
                case 1:
                    {
                        MessageBox.Show("注册成功！");
                        this.button1.Enabled = false; //注册成功时，注册按钮不可点
                        axFramerCS_One = new AxFramerControlSetter(new AxDSOFramer.AxFramerControl());
                        this.button3.Enabled = true;
                        this.button4.Enabled = true;
                        this.button5.Enabled = true;
                        break;
                    }
                case 2:
                    MessageBox.Show("已注册过，不用重复注册！");
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //取消注册dsoframer.ocx
            bool result = OcxRegister.UnRegisterMain();
            if (result)
            {
                MessageBox.Show("取消注册成功！");
                this.button1.Enabled = true;
                axFramerCS_One = null;
                this.button3.Enabled = false;
                this.button4.Enabled = false;
                this.button5.Enabled = false;
            }
            else
            {
                MessageBox.Show("dsoframer.ocx并未注册，无法取消！");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            // 加载ppt
            if (axFramerCS_One.IsInit == true && axFramerCS_One != null)
            {
                this.axFramerCS_One.Close();
            }
            string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "//testfiles//测试ppt.pptx";
            axFramerCS_One.Init(filepath, this.panel1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // 加载excel
            if (axFramerCS_One.IsInit == true && axFramerCS_One != null)
            {
                this.axFramerCS_One.Close();
            }
            string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "//testfiles//测试excel.xlsx";
            axFramerCS_One.Init(filepath, this.panel1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            // 加载word
            if (axFramerCS_One.IsInit == true && axFramerCS_One != null)
            {
                this.axFramerCS_One.Close();
            }
            string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "//testfiles//测试word.docx";
            axFramerCS_One.Init(filepath, this.panel1);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭窗口前，要做的事情
            if (axFramerCS_One == null)
                return;
            try
            {
                if (axFramerCS_One.IsInit == true && axFramerCS_One != null)
                {
                    this.axFramerCS_One.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
